using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.SdkTypeProxy.Metadata;

using System.Xml;
using System.IO;

namespace winCRM4QueryBuilder
{
    public partial class frmCRM4XMLQueryTestQuery : Form
    {
        appsets myappsets = new appsets();

        public frmCRM4XMLQueryTestQuery()
        {
            InitializeComponent();
        }

        public frmCRM4XMLQueryTestQuery(string sTempStr)
        {
            InitializeComponent();
            sQuery = sTempStr;
            if (sQuery == "")
            {
            }
            else
            {
                this.richTextBox1.Text = sQuery;
            }
        }

        public string sQuery = string.Empty;

        private void btnFetchAll_Click(object sender, EventArgs e)
        {
            DataTable mydtoutput = new DataTable();
            try
            {
                string fetch1 = richTextBox1.Text.ToString();
                CrmService myServicew = CrmServiceUtility.GetCrmService(myappsets.crmserverurl, myappsets.crmog);
                ExecuteFetchRequest req = new ExecuteFetchRequest();
                req.FetchXml = fetch1;
                ExecuteFetchResponse results1 = (ExecuteFetchResponse)myServicew.Execute(req);
                StringBuilder mysb1 = new StringBuilder();

                XmlTextReader reader = new XmlTextReader(results1.FetchXmlResult, XmlNodeType.Document, null);
                mysb1.Append(@"<?xml version='1.0' standalone='yes' ?>");
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            mysb1.Append("<" + reader.Name + ">");
                            break;
                        case XmlNodeType.Text:
                            mysb1.Append(reader.Value);
                            break;
                        case XmlNodeType.EndElement:
                            mysb1.Append("</" + reader.Name + ">");
                            break;
                    }
                }
                //! IMPORTANT: SOME SPECIAL CHARACTERS IN THE RETURNED RESULTS STRING NEEDED TO BE REMOVED
                string stest = mysb1.ToString().Replace("&", "&amp;");
                StringReader xmlst2 = new StringReader(stest);
                DataSet myds2 = new DataSet();
                myds2.ReadXml(xmlst2);
                this.dataGridView1.DataSource = myds2.Tables[0];

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void frmCRM4XMLQueryTestQuery_Load(object sender, EventArgs e)
        {
           
        }
    }
}